//
//  LBMediaPresentViewController.m
//  LangBa
//
//  Created by Mac on 2017/12/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BBHomeMenuPresentViewController.h"
#import "TMTabbarViewController.h"
@interface BBHomeMenuPresentViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;


@end

@implementation BBHomeMenuPresentViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth, 342)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = UIColor.whiteColor;
    if ([BBThemeColorManger manger].type == BBThemeColorStyleBlacklook) {
      self.customView.backgroundColor = FlatGray;
    }
    [self.clearButton addSubview:self.customView];
    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedWhiteView)];
    self.customView.userInteractionEnabled = YES;
    [self.customView addGestureRecognizer:ges];
    
    BBUserModel * user = [STUserManger defult].loginedUser;
    //sub

    STButton * iconButton = [[STButton alloc] initWithFrame:CGRectMake(30, -20, 60, 60)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:30
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    [iconButton sd_setImageWithURL:[NSURL URLWithString:user.avatar] forState:UIControlStateNormal];
    [self.customView addSubview:iconButton];
    [iconButton setClicAction:^(UIButton *sender) {
        [weakSelf dismissWithHandle:^{
            if (weakSelf.onSelctedMenuButton) {
                weakSelf.onSelctedMenuButton(@"头像");
            }
        }];
    }];
    [iconButton showRoundBackViewWithColor:[UIColor whiteColor]];
    
    
    STLabel * coinLable = [[STLabel alloc] initWithFrame:CGRectMake(iconButton.right + 10, 0, 200, 44)
                                                     text:[NSString stringWithFormat:@"当前价值K%@",user.cost]
                                                textColor:TM_redColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [self.customView addSubview:coinLable];
    coinLable.hidden = !user;
    NSString * title = @"夜间模式";
    if ([BBThemeColorManger manger].type == BBThemeColorStyleBlacklook) {
        title = @"日间模式";
    }
    NSArray * titles = @[@"收藏网址",title,@"刷新",@"收藏/历史",@"退出"];
    NSArray * images = @[@"收藏",@"夜间模式",@"刷新",@"历史收藏",@"quit"];
    CGFloat menuWith = UIScreenWidth / titles.count;
    for (NSInteger i = 0; i < titles.count; i ++) {
        NSString * title = titles[i];
        NSString * imageName = images[i];
        UIColor * titleColor = FirstTextColor;
        TMTabbarViewController * vc = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
        UINavigationController * nav = vc.childViewControllers.firstObject;
        if (![nav.childViewControllers.lastObject isKindOfClass:[STWebViewController class]] &&
            ![nav.childViewControllers.lastObject isKindOfClass:[BBWebViewController class]] &&
            [title isEqualToString:@"收藏网址"]) {
            titleColor = SecendTextColor;
        }
        STMenuControl * menu = [[STMenuControl alloc] initWithFrame:CGRectMake(0, iconButton.bottom + 15, menuWith, menuWith + 30) imageName:imageName title:title titleColor:titleColor];
        menu.userInteractionEnabled = YES;
        [menu setOnSelctedControl:^(STMenuControl *control) {
            NSLog(@"%@",title);
            [weakSelf dismissWithHandle:^{
                if (weakSelf.onSelctedMenuButton) {
                    weakSelf.onSelctedMenuButton(title);
                }
            }];
        }];
        menu.left = i * menuWith;
        
        [self.customView addSubview:menu];

    }
    self.customView.userInteractionEnabled = YES;
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, iconButton.bottom + 45 + menuWith + 1  , UIScreenWidth, 1)];
    line.backgroundColor = TM_lineColor;
    [self.customView addSubview:line];
    
    CGFloat buttonWith = UIScreenWidth / 3;
    STButton * setButton = [[STButton alloc] initWithFrame:CGRectMake(0, line.bottom, buttonWith, 44)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"设置"]];
    setButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [setButton setClicAction:^(UIButton *sender) {
        [weakSelf dismissWithHandle:^{
            if (weakSelf.onSelctedMenuButton) {
                weakSelf.onSelctedMenuButton(@"设置");
            }
        }];
    }];
    [self.customView addSubview:setButton];
    
   
    STButton * rollButton = [[STButton alloc] initWithFrame:CGRectMake(buttonWith, line.bottom, buttonWith, 44)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"返回1"]];
    rollButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [rollButton setClicAction:^(UIButton *sender) {
        [weakSelf dismissWithHandle:^{

        }];
    }];
    [self.customView addSubview:rollButton];
    
    STButton * shareButton = [[STButton alloc] initWithFrame:CGRectMake(2*buttonWith, line.bottom, buttonWith, 44)
                                                      title:nil
                                                 titleColor:nil
                                                  titleFont:0
                                               cornerRadius:0
                                            backgroundColor:nil
                                            backgroundImage:nil
                                                      image:[UIImage imageNamed:@"分享"]];
    shareButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.customView addSubview:shareButton];
    [shareButton setClicAction:^(UIButton *sender) {
        [weakSelf dismissWithHandle:^{
            if (weakSelf.onSelctedMenuButton) {
                weakSelf.onSelctedMenuButton(@"分享");
            }
        }];
    }];
    
    self.customView.height = shareButton.bottom;
    self.customView.top = UIScreenHeight;
}
#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.bottom = UIScreenHeight;
                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)dismissWithHandle:(void(^)())handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}
#pragma mark --Action Method

- (void)onSelectedCancleButton{
    
    [self dismiss];
    
}
- (void)onSelectedWhiteView{
    [self.view endEditing:YES];
}


@end



